package com.github.wxiaoqi.security.biz.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.wxiaoqi.security.biz.biz.BhFunctionBiz;
import com.github.wxiaoqi.security.biz.biz.BhIndustryBiz;
import com.github.wxiaoqi.security.biz.biz.BhLableBiz;
import com.github.wxiaoqi.security.biz.biz.BhTaskBiz;
import com.github.wxiaoqi.security.biz.biz.BhTaskTagBiz;
import com.github.wxiaoqi.security.biz.entity.BhFunction;
import com.github.wxiaoqi.security.biz.entity.BhIndustry;
import com.github.wxiaoqi.security.biz.entity.BhTask;
import com.github.wxiaoqi.security.biz.entity.BhTaskTag;
import com.github.wxiaoqi.security.common.msg.ResponseCode;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.util.EntityUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("bhTask")
public class TaskAction {
	@Autowired
	private BhTaskTagBiz bhTaskTagBiz;
	@Autowired
	private BhTaskBiz bhTaskBiz;
	@Autowired
	private BhLableBiz bhLableBiz;
	@Autowired
	private BhIndustryBiz bhIndustryBiz;
	@Autowired
	private BhFunctionBiz bhFunctionBiz;
	
	/**
	 * 保存不发布,编辑不发布
	 */
	@RequestMapping(value = "/addTask", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addTask(@RequestBody Map<String, Object> reqs) {
		log.info("保存不发布,编辑不发布addTask传入参数{}", reqs);
		Map<String, Object> result = bhTaskBiz.saveOrEditTask(reqs);
		log.info("保存不发布,编辑不发布addTask结束{}", result);
		return result;
		}
	
	/**
	 * 编辑查询一个任务信息
	 * 
	 */
	@RequestMapping(value = "/selectOneTask", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectOneTask(@RequestBody Map<String, Object> reqs) throws Exception{
		log.info("编辑查询一个任务信息 - bhTask - selectOneTask 传入参数{}", reqs);
		Map<String, Object> result = new HashMap<String, Object>();
		String id = (String) reqs.get("id");
		log.info("编辑查询一个任务信息 - bhTask - selectOneTask id{}", id);
		if("".equals(id) || null == id){
			result.put("code", ResponseCode.TASK_NOTEXIST.getCode());
			result.put("msg", ResponseCode.TASK_NOTEXIST.getMessage());
			log.info("编辑查询一个任务信息 - bhTask - selectOneTask结束{}", result);
			return result;
		}
		BhTask bh = bhTaskBiz.selectById(id);
		BhTaskTag taskTag = new BhTaskTag();
		taskTag.setTaskId(bh.getId());
		List<BhTaskTag> list = bhTaskTagBiz.selectList(taskTag);
		String tag = "";
		for (BhTaskTag bhTaskTag : list) {
			tag = bhTaskTag.getTagId() + ",";
		}
		log.info("编辑查询一个任务信息 - bhTask - selectOneTask tag{}", tag);
		bh.setLable(tag);
		//Map<String, Object> bhMap = (Map<String, Object>) BeanUtils.ConvertObjToMap(bh);
		Map<String, Object> bhMap = EntityUtils.beanToMap(bh);
		//取出一级行业跟一级职能
		BhIndustry industry = bhIndustryBiz.selectById(bh.getCompanyIndustry());
		BhFunction function = bhFunctionBiz.selectById(bh.getCompanyFunction());
		bhMap.put("oneIndustry", industry.getParentid());
		bhMap.put("oneFunction", function.getParentid());
		result.put("code", ResponseCode.OK.getCode());
		result.put("msg", ResponseCode.OK.getMessage());
		result.put("body", bhMap);
		log.info("编辑查询一个任务信息 - bhTask - selectOneTask结束{}", result);
		return result;
		
	}
	/**
	 * 分页查询
	 * page
	 * limit
	 */
	@RequestMapping(value = "/queryTaskPage", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryTaskPage(@RequestBody Map<String, Object> reqs) {
		log.info("分页查询任务信息 - bhTask - queryTaskPage 传入参数{}", reqs);
		Map<String, Object> result = new HashMap<String, Object>();
		TableResultResponse<BhTask> response = null;
		try {
			response = bhTaskBiz.queryTaskPage(reqs);
			result.put("body", response);
			result.put("code", ResponseCode.OK.getCode());
			result.put("sucess", true);
			result.put("msg", ResponseCode.OK.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseCode.ABNORMAL_FIELDS.getCode());
			result.put("msg", ResponseCode.ABNORMAL_FIELDS.getMessage());
			result.put("sucess", false);
			result.put("body", null);
		}
		log.info("分页查询任务信息 - bhTask - queryTaskPage 结束{}", result);
		return result;
	}
	/**
	 * 直接发布，编辑后发布
	 */
	@RequestMapping(value = "/publishTask", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> publishTask(@RequestBody Map<String, Object> reqs) {
		log.info("直接发布，编辑后发布 - bhTask - publishTask 参数传入{}", reqs);
		Map<String, Object> result = new HashMap<String, Object>();
		String id = (String) reqs.get("id");
		if(id == null || id.equals("id")){
			//直接发布
			log.info("直接发布，编辑后发布 - bhTask - publishTask 直接发布");
			result = bhTaskBiz.directublish(reqs);
		}else {
			//编辑后发布
			log.info("直接发布，编辑后发布 - bhTask - publishTask 编辑后发布");
			result = bhTaskBiz.editpublish(reqs);
		}
		log.info("直接发布，编辑后发布 - bhTask - publishTask 结束{}", result);
		return result;
	}
}
